package bit.springblogdemo.service.impl;

import bit.springblogdemo.common.exception.BlogException;
import bit.springblogdemo.common.util.BeanUtilTrans;
import bit.springblogdemo.mapper.BlogInfoMapper;
import bit.springblogdemo.pojo.dataobject.BlogInfo;
import bit.springblogdemo.pojo.request.AddBlogRequest;
import bit.springblogdemo.pojo.request.UpdateBlogRequest;
import bit.springblogdemo.pojo.response.BlogInfoResponse;

import bit.springblogdemo.service.BlogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,0);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);//数据库查询信息
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogListResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogListResponse);
            return blogListResponse;
        }).collect(Collectors.toList());
        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(@NotNull Integer blogId) {
//        if(blogId==null){
//            return null;
//        }

        //把下面的代码精简一下
//        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(BlogInfo::getId,blogId).eq(BlogInfo::getDeleteFlag,1);
//        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
//        return  BeanUtilTrans.trans(blogInfo);

        BlogInfo blogInfo = getBlogInfo(blogId);
        return BeanUtilTrans.trans(blogInfo);

    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getId,blogId).eq(BlogInfo::getDeleteFlag,0);
        BlogInfo blogInfo = blogInfoMapper.selectOne(queryWrapper);
        return  blogInfo;
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try{
            Integer result = blogInfoMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
            return false;
        }catch(Exception e){
            log.error("博客插入失败,e:"+e);
            throw new BlogException("内部错误，请联系管理员");
        }

    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest,blogInfo);
        try{
            int result = blogInfoMapper.updateById(blogInfo);
            if(result==1){
                return true;
            }

        }catch(Exception e){
            log.error("修改博客发生异常，e:",e);
        }
         return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setDeleteFlag(1);
        blogInfo.setId(blogId);
        try{
            int result = blogInfoMapper.updateById(blogInfo);
            if(result==1){
                return true;
            }
        }catch(Exception e){
            log.error("删除博客失败,e:",e);
        }
        return false;
    }


}
